var map;
var zoom = 12;
var newControl;
var countries = [];
var countriesOverlay = new T.D3Overlay(init,redraw);
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return null;
}
var imageURL = "http://t0.tianditu.cn/img_w/wmts?" +
    "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
    "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}";
//创建自定义图层对象
var lay = new T.TileLayer(imageURL, {minZoom: 1, maxZoom: 18});

// var markers;
// var countriesOverlay1 = new T.D3Overlay(init1,redraw1);
function onLoad() {
    var type = GetQueryString("type");

    newControl = new T.Control({position: T_ANCHOR_TOP_LEFT});
    newControl.onAdd = function (map) {
        var container = document.createElement("div");
        var zicsstext = "font-size:12px;border:solid 2px blue;background:#fff;padding:2px;line-height:15px;cursor:pointer;";
        var zocsstext = "font-size:12px;border:solid 2px blue;background:#fff;padding:2px;line-height:15px;cursor:pointer;";
        var tccsstext = "font-size:12px;border:solid 2px blue;background:#fff;padding:2px;line-height:15px;cursor:pointer;position:absolute;right:-890px;top:505px";
        var tccsstext1 = "font-size:12px;border:solid 2px blue;background:#fff;padding:2px;line-height:15px;cursor:pointer;position:absolute;right:-933px;top:505px";
        this.ZQButton = createButton("水文", "水文", 'a', container, zicsstext);
        this.ZZButton = createButton("水位", "水位", 'b', container, zocsstext);
        this.PPButton = createButton("降水", "降水", 'b', container, zocsstext);
        this.OtButton = createButton("其他", "其他", 'b', container, zocsstext);
        this.SLTButton = createButton("矢量图", "矢量图", 'a', container, tccsstext);
        this.YXTButton = createButton("影像图", "影像图", 'b', container, tccsstext1);
        this.SLTButton.onclick = showSLT;
        this.YXTButton.onclick = showYXT;
        this.ZQButton.onclick = showQ;
        this.ZZButton.onclick = showZ;
        this.PPButton.onclick = showP;
        this.OtButton.onclick = showOth;
        return container;
    };

    newControl.onRemove = function (map) {
        //移除控件时要释放
        delete  this.ZQButton;
        delete  this.ZZButton;
        delete  this.PPButton;
        delete  this.SLTButton;
        delete  this.YXTButton;
    }

    if (type == "duo") {
        //一个或着多个
        var iids = GetQueryString("iids");
        $.get("/qtIndex/map/data.action?iids=" + iids, function (data, status) {
            map = new T.Map('mapDiv');
            // d3.json("/json/shuixi.json", function (data) {
            //     countries = data.features;
            //     map.addOverLay(countriesOverlay)
            //     countriesOverlay.bringToBack();
            // });
            // var c = null;
            // var imageURL = "http://t0.tianditu.cn/img_w/wmts?" +
            //     "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
            //     "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}";
            // //创建自定义图层对象
            // var lay = new T.TileLayer(imageURL, {minZoom: 1, maxZoom: 18});
            //将图层增加到地图上
            map.addLayer(lay);
            map.centerAndZoom(new T.LngLat(data[0].lgtd, data[0].lttd), zoom);
            map.addControl(newControl);
            map.on('mousemove',getLngLat);

            for (var i = 0; i < data.length; i++) {
                // alert(data[i].stnm+" "+data[i].lgtd+" "+data[i].lttd);
                var icon = null;
                if (data[i].sttpcd == "PP") {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/PP.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                } else if (data[i].sttpcd == "ZQ") {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/ZQ.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                } else if (data[i].sttpcd == "ZZ") {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/ZZ.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                } else {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/ZQ.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                }
                //创建标注对象
                var marker = new T.Marker(new T.LngLat(data[i].lgtd, data[i].lttd), {icon: icon});
                //向地图上添加标注
                map.addOverLay(marker);
                var content = data[i].stnm;
                addClickHandler(content, marker);
            }

        });
    } else {
        //type = all或着默认
        $.get("/qtIndex/map/data.action?iids=all/"+GetQueryString("id"), function (data, status) {
            // alert(data.length);
            map = new T.Map('mapDiv', {
                attributionControl: false,
                inertia: false
            });
            //将图层增加到地图上
            map.addLayer(lay);
            map.centerAndZoom(new T.LngLat(121.56, 29.86), 8);
            map.addControl(newControl);
            map.on('mousemove',getLngLat);
            var arrayObj = new Array();
            for (var i = 0; i < data.length; i++) {
                // alert(data[i].stnm+" "+data[i].lgtd+" "+data[i].lttd);
                var icon = null;
                //创建标注对象
                if (data[i].sttpcd == "PP") {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/PP.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                } else if (data[i].sttpcd == "ZQ") {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/ZQ.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                } else if (data[i].sttpcd == "ZZ") {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/ZZ.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                } else {
                    icon = new T.Icon({
                        iconUrl: "/jsps/qt/images/ZQ.png",
                        iconSize: new T.Point(18, 18),
                        iconAnchor: new T.Point(10, 25)
                    });
                }
                var marker = new T.Marker(new T.LngLat(data[i].lgtd, data[i].lttd), {icon: icon});
                //向地图上添加标注
                // arrayObj.push(marker);
                map.addOverLay(marker);
                var content = data[i].stnm;
                addClickHandler(content, marker);
            }
            // var marker = new T.Marker(new T.LngLat(Math.random() * 40 + 85, Math.random() * 30 + 21), {title: i});
            // arrayObj.push(marker);
            // addClickHandler(i + "1231231", marker);
            // markers = new T.MarkerClusterer(map, {markers: arrayObj});
        });


    }

}

function showSLT() {
    map.removeLayer(lay);
}

function showYXT() {
    map.addLayer(lay);
}

function showQ() {
    map.clearOverLays();
    // alert("QQ");
    showCz("ZQ");
}
function showZ() {
    map.clearOverLays();
    // alert("ZZ");
    showCz("ZZ");

}
function showP() {
    map.clearOverLays();
    // alert("PP");
    showCz("PP");

}
function showOth() {
    map.clearOverLays();
    showCz("Oth");

}

function showCz(CzType) {
    var type = GetQueryString("type");
    if(CzType=="Oth"){
        if (type == "duo") {
            //一个或着多个
            var iids = GetQueryString("iids");
            $.get("/qtIndex/map/data.action?iids=" + iids, function (data, status) {
                // map = new T.Map('mapDiv');
                map.centerAndZoom(new T.LngLat(data[0].lgtd, data[0].lttd), zoom);
                map.addControl(newControl);
                map.enableDrag();
                for (var i = 0; i < data.length; i++) {
                    // alert(data[i].stnm+" "+data[i].lgtd+" "+data[i].lttd);
                    if(data[i].sttpcd!="PP"&&data[i].sttpcd!="ZQ"&&data[i].sttpcd!="ZZ"){
                        var icon = new T.Icon({
                            iconUrl: "/jsps/qt/images/ZQ.png",
                            iconSize: new T.Point(18, 18),
                            iconAnchor: new T.Point(10, 25)
                        });
                        //创建标注对象
                        var marker = new T.Marker(new T.LngLat(data[i].lgtd, data[i].lttd), {icon: icon});
                        //向地图上添加标注
                        map.addOverLay(marker);
                        var content = data[i].stnm;
                        addClickHandler(content, marker);
                    }
                }

            });
        } else {
            //type = all或着默认
            $.get("/qtIndex/map/data.action?iids=all/"+GetQueryString("id"), function (data, status) {
                // markers.clearMarkers();
                // alert(data.length);
                map = new T.Map('mapDiv', {
                    attributionControl: false,
                    inertia: false
                });
                map.centerAndZoom(new T.LngLat(121.56, 29.86), 8);
                map.addControl(newControl);
                map.enableDrag();
                var arrayObj = new Array();
                for (var i = 0; i < data.length; i++) {
                    // alert(data[i].stnm+" "+data[i].lgtd+" "+data[i].lttd);
                    if(data[i].sttpcd!="PP"&&data[i].sttpcd!="ZQ"&&data[i].sttpcd!="ZZ"){
                        var icon = new T.Icon({
                            iconUrl: "/jsps/qt/images/ZQ.png",
                            iconSize: new T.Point(18, 18),
                            iconAnchor: new T.Point(10, 25)
                        });
                        var marker = new T.Marker(new T.LngLat(data[i].lgtd, data[i].lttd), {icon: icon});
                        //向地图上添加标注
                        // arrayObj.push(marker);
                        map.addOverLay(marker);
                        var content = data[i].stnm;
                        addClickHandler(content, marker);
                    }
                }
                // markers = new T.MarkerClusterer(map, {markers: arrayObj});
            });


        }
    }else {
        if (type == "duo") {
            //一个或着多个
            var iids = GetQueryString("iids");
            $.get("/qtIndex/map/data.action?iids=" + iids, function (data, status) {
                // map = new T.Map('mapDiv');
                map.clearOverLays();
                map.centerAndZoom(new T.LngLat(data[0].lgtd, data[0].lttd), zoom);
                map.addControl(newControl);

                // map.enableDrag();
                for (var i = 0; i < data.length; i++) {
                    // alert(data[i].stnm+" "+data[i].lgtd+" "+data[i].lttd);
                    if(data[i].sttpcd==CzType){
                        var icon = null;
                        if (data[i].sttpcd == "PP") {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/PP.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        } else if (data[i].sttpcd == "ZQ") {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/ZQ.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        } else if (data[i].sttpcd == "ZZ") {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/ZZ.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        } else {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/ZQ.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        }
                        //创建标注对象
                        var marker = new T.Marker(new T.LngLat(data[i].lgtd, data[i].lttd), {icon: icon});
                        //向地图上添加标注
                        map.addOverLay(marker);
                        var content = data[i].stnm;
                        addClickHandler(content, marker);
                    }
                }

            });
        } else {
            //type = all或着默认
            $.get("/qtIndex/map/data.action?iids=all/"+GetQueryString("id"), function (data, status) {

                // alert(data.length);
                // map = new T.Map('mapDiv', {
                //     attributionControl: false,
                //     inertia: false
                // });
                // markers.clearMarkers();
                map.clearOverLays();
                map.centerAndZoom(new T.LngLat(121.56, 29.86), 8);
                map.addControl(newControl);
                // alert(map.isDrag());
                var arrayObj = new Array();
                for (var i = 0; i < data.length; i++) {
                    // alert(data[i].stnm+" "+data[i].lgtd+" "+data[i].lttd);
                    if(data[i].sttpcd==CzType) {
                        var icon = null;
                        //创建标注对象
                        if (data[i].sttpcd == "PP") {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/PP.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        } else if (data[i].sttpcd == "ZQ") {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/ZQ.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        } else if (data[i].sttpcd == "ZZ") {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/ZZ.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        } else {
                            icon = new T.Icon({
                                iconUrl: "/jsps/qt/images/ZQ.png",
                                iconSize: new T.Point(18, 18),
                                iconAnchor: new T.Point(10, 25)
                            });
                        }
                        var marker = new T.Marker(new T.LngLat(data[i].lgtd, data[i].lttd), {icon: icon});
                        //向地图上添加标注
                        // arrayObj.push(marker);
                        map.addOverLay(marker);
                        var content = data[i].stnm;
                        addClickHandler(content, marker);
                    }
                }
                // markers = new T.MarkerClusterer(map, {markers: arrayObj});
            });


        }
    }

}
function createButton(html, title, className, container, csstext) {
    var link = document.createElement("a");
    if (container) {
        container.appendChild(link);
    }
    link.innerHTML = html;
    link.title = title;
    link.style.cssText = csstext;
    return link;
}
function addClickHandler(content, marker) {
    marker.addEventListener("click", function (e) {
            openInfo(content, e)
        }
    );
}
function openInfo(content, e) {
    var point = e.lnglat;
    marker = new T.Marker(point);// 创建标注
    var markerInfoWin = new T.InfoWindow(content, {offset: new T.Point(0, -30)}); // 创建信息窗口对象
    map.openInfoWindow(markerInfoWin, point); //开启信息窗口
}


function getLngLat(type, target, lnglat, containerPoint) {
    document.getElementById("lnglatStr").value = type.lnglat.lng.toFixed(6) + "," + type.lnglat.lat.toFixed(6);
}
function init(sel, transform) {
    var upd = sel.selectAll('path.geojson').data(countries);
    upd.enter()
        .append('path')
        .attr("class", "geojson")
        .attr('stroke', 'black')
        .attr('fill', function (d, i) {
            return d3.hsl(Math.random() * 360, 0.9, 0.5)
        })
        .attr('fill-opacity', '0.5')


}

function redraw(sel, transform) {
    sel.selectAll('path.geojson').each(
        function (d, i) {
            d3.select(this).attr('d', transform.pathFromGeojson)
        }
    )

}


// function init1(sel, transform) {
//     var upd = sel.selectAll('path.geojson1').data(countries);
//     upd.enter()
//         .append('path')
//         .attr("class", "geojson1")
//         .attr('stroke', 'black')
//         .attr('fill', function (d, i) {
//             return d3.hsl(Math.random() * 360, 0.9, 0.5)
//         })
//         .attr('fill-opacity', '0.5')
//
//
// }
//
// function redraw1(sel, transform) {
//     sel.selectAll('path.geojson1').each(
//         function (d, i) {
//             d3.select(this).attr('d', transform.pathFromGeojson)
//         }
//     )
//
// }
